#include "LoginDialog.h"

#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QMessageBox>

LoginDialog::LoginDialog(QWidget* parent) : QDialog(parent) {
    userLabel = new QLabel(this);
    userLabel->move(70, 80);
    userLabel->setText(tr("用户名："));

    userLineEdit = new QLineEdit(this);
    userLineEdit->move(140, 80);
    userLineEdit->setPlaceholderText(tr("请输入用户名："));

    passwordLabel = new QLabel(this);
    passwordLabel->move(70, 130);
    passwordLabel->setText(tr("密码："));

    passwordLineEdit = new QLineEdit(this);
    passwordLineEdit->move(140, 130);
    passwordLineEdit->setPlaceholderText(tr("请输入密码："));
    passwordLineEdit->setEchoMode(QLineEdit::Password);

    loginButton = new QPushButton(this);
    loginButton->move(50, 200);
    loginButton->setText(tr("登录"));

    exitButton = new QPushButton(this);
    exitButton->move(210, 200);
    exitButton->setText(tr("退出"));

    connect(loginButton, &QPushButton::clicked, this, &LoginDialog::login);
    connect(exitButton, &QPushButton::clicked, this, &LoginDialog::close);
}

LoginDialog::~LoginDialog() {
}

void LoginDialog::login() {
    // 判断用户名和密码是否正确
    // 如果错误则弹出警告对话框
    if (userLineEdit->text().trimmed() == tr("tengyft") &&
        passwordLineEdit->text() == tr("123456")) {
        accept();
    } else {
        QMessageBox::warning(this, tr("警告！"), tr("用户名或密码错误！"),
                             QMessageBox::Yes);
        // 清空内容并定位光标
        userLineEdit->clear();
        passwordLineEdit->clear();
        userLineEdit->setFocus();
    }
}
